﻿/**************************************************
*审核模块
 * 数据访问层  
 * 评估功能、列表展示、审核、修改状态
 * Author：JXL
 
 *************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class AuditDAL : IAuditDAL
    {

        public static string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        /// <summary>
        /// 显示审核列表
        /// </summary>
        /// <returns></returns>
        public pagelist<Basic_infoss> getlist(int pageindex, int pagesize, string wherestr)
        {
            string tablename = " Basic_Info a,users b,users c ,users d";
            string strwhere = " a.Survey=b.Users_Id and a.Evaluation=c.Users_Id and a.basic_CreatePeople=d.Users_Id and basic_State>2" + wherestr;
            string strid = "a.basic_CreateTime,a.basic_id,a.projectnumber,b.users_name AuditSurvey,c.users_name AuditEvaluation,a.House_Located,a.Basic_Acreage,a.Basic_UnitPrice,a.Basic_Total,a.Basic_State,d.users_name";
            string sort = "basic_id";
            using (SqlConnection sql = new SqlConnection(HouseCommon.Common.conn))
            {
                sql.Open();
                SqlCommand cmd = new SqlCommand("Common_PageList", sql);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter[] pa ={
                                     new SqlParameter("@tab",SqlDbType.NVarChar,100),
                                     new SqlParameter("@pageindex",SqlDbType.Int),
                                     new SqlParameter("@pagesize",SqlDbType.Int),
                                     new SqlParameter("@pagecount",SqlDbType.Int),
                                     new SqlParameter("@strFld",SqlDbType.NVarChar,500),
                                     new SqlParameter("@strWhere",SqlDbType.NVarChar,500),
                                     new SqlParameter("@Sort",SqlDbType.NVarChar,50)
                                 };
                pa[0].Value = tablename;
                pa[1].Value = pageindex;
                pa[2].Value = pagesize;
                pa[3].Direction = ParameterDirection.Output;
                pa[4].Value = strid;
                pa[5].Value = strwhere;
                pa[6].Value = sort;
                cmd.Parameters.AddRange(pa);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable ta = new DataTable();
                da.Fill(ta);
                List<Basic_infoss> list = DBhelper.DBHelper.DataTable2List<Basic_infoss>(ta).ToList();

                pagelist<Basic_infoss> liss = new pagelist<Basic_infoss>();
                liss.getlist = list;
                liss.pagecount = (int)pa[3].Value;
                return liss;
            }

        }
        public List<Basic_infoss> AuditList(string pronum)
        {
            return null;
            #region
            //using (SqlConnection conn = new SqlConnection(str))
            //{
            //    conn.Open();
            //    string sql = "";
            //    if (pronum == "" || pronum == null)
            //    {
            //        sql = "select basic_CreateTime,basic_id,projectnumber, AuditSurvey=(select users_name from Users where Basic_Info.Survey=Users.Users_Id),AuditEvaluation=(select users_name from users where Users.Users_Id=Basic_Info.Evaluation),House_Located,Basic_Acreage,Basic_UnitPrice,Basic_Total,Basic_State from Basic_Info";
            //    }
            //    else
            //    {
            //        sql = "select basic_CreateTime,basic_id,projectnumber, AuditSurvey=(select users_name from Users where Basic_Info.Survey=Users.Users_Id),AuditEvaluation=(select users_name from users where Users.Users_Id=Basic_Info.Evaluation),House_Located,Basic_Acreage,Basic_UnitPrice,Basic_Total,Basic_State from Basic_Info where ProjectNumber='" + pronum + "'";
            //    }

            //    SqlCommand cmd = new SqlCommand(sql, conn);

            //    SqlDataReader reader = cmd.ExecuteReader();

            //    List<Basic_infoss> list = new List<Basic_infoss>();

            //    if (reader.HasRows)
            //    {
            //        while (reader.Read())
            //        {
            //            Basic_infoss bas = new Basic_infoss();
            //            bas.projectNumber = reader["projectNumber"].ToString();

            //            bas.basic_CreatePeople = Convert.ToInt32(reader["basic_CreatePeople"]);
            //            bas.AuditEvaluation = reader["AuditEvaluation"].ToString();
            //            bas.AuditSurvey = reader["AuditSurvey"].ToString();
            //            bas.house_Located = reader["house_Located"].ToString();
            //            bas.basic_Acreage = Convert.ToDecimal(reader["basic_Acreage"]);
            //            bas.basic_Total = Convert.ToDecimal(reader["basic_Total"]);
            //            bas.basic_UnitPrice = Convert.ToDecimal(reader["basic_UnitPrice"]);
            //            bas.basic_State = Convert.ToInt32(reader["basic_State"]);
            //            bas.basic_id = Convert.ToInt32(reader["basic_id"]);
            //            bas.basic_CreateTime = Convert.ToDateTime(reader["basic_CreateTime"]);

            //            list.Add(bas);
            //        }
            //    }



            //    return list;
            //}
            #endregion
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="bas"></param>
        /// <returns></returns>
        public int Audit(Basic_infoss bas,int id)
        {
            using (SqlConnection conn = new SqlConnection(str))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("AuditProc", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter[] par ={
                                       new SqlParameter("@basic_id",SqlDbType.Int),
                                       new SqlParameter("@Basic_State",SqlDbType.Int),
                                       new SqlParameter("@ProjectNumber",SqlDbType.NVarChar),
                                       new SqlParameter("@Auditing_Opinion",SqlDbType.NVarChar),
                                       new SqlParameter("@Auditing_Time",SqlDbType.DateTime),
                                       new SqlParameter("@Auditing_Type",SqlDbType.NVarChar),
                                       new SqlParameter("@Auditing_CreateTime",SqlDbType.DateTime),
                                       new SqlParameter("@uid",SqlDbType.Int),
                                       //new SqlParameter("@Auditing_CreateTime",SqlDbType.DateTime),
                                   };
                if (bas.Auditing_Opinion == null)
                {
                    bas.Auditing_Opinion = "";
                }
                par[0].Value = bas.basic_id;
                par[1].Value = bas.basic_State;
                par[2].Value = bas.projectNumber;
                par[3].Value = bas.Auditing_Opinion;
                par[4].Value = bas.auditing_Time;
                par[5].Value = bas.auditing_Type;
                par[6].Value = bas.basic_CreateTime;
                par[7].Value = id;
                cmd.Parameters.AddRange(par);

                return cmd.ExecuteNonQuery();

            }
        }


        /// <summary>
        /// 当状态‘初审已通过’1再点击 审核不通过 的时候状态改为‘终审未通过’2
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int NoAudit(int id,int state)
        {
            using (SqlConnection conn = new SqlConnection(str))
            {
                string sql = "";
                conn.Open();
                if(state==3)
                {
                     sql = "update Basic_Info set Basic_State=4 where Basic_id=" + id + "";
                }
                if(state==5)
                {
                    sql = "update Basic_Info set Basic_State=6 where Basic_id=" + id + "";
                }
                
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteNonQuery();
            }
        }
    }
}
